package com.yyyzzz.servlet;

import com.yyyzzz.bean.Course;
import com.yyyzzz.service.courseService;
import com.yyyzzz.service.impl.courseServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/addCourseServlet")
public class addCourseServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String courseId = request.getParameter("courseId");
        String courseName = request.getParameter("courseName");
        String courseTeacher = request.getParameter("courseTeacher");
        String classroom = request.getParameter("classroom");
        Double credit = Double.parseDouble(request.getParameter("credit"));
        response.setCharacterEncoding("utf-8");


        Course course = new Course();
        course.setCourseId(courseId);
        course.setCourseName(courseName);
        course.setCourseTeacher(courseTeacher);
        course.setClassroom(classroom);
        course.setCredit(credit);


        HttpSession session = request.getSession();

        courseService service = new courseServiceimpl();
        boolean addCourse = service.addCourse(course);
        if (addCourse == true) {
            List<Course> courseList = service.getAll();
            request.setAttribute("courseList", courseList);
          /*  for(Course course1: courseList) {
                System.out.println(course1);
            }*/
            request.getRequestDispatcher("courseDetail.jsp").forward(request, response);
        } else {
            session.setAttribute("message", "添加课程失败");
            request.getRequestDispatcher("addCourse.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
